package day8;

import banking.domain.OverdraftException;

public class Account {
    // 修改 balance 属性为 protected
    protected double balance;

    // 公共构造器
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    // 获取余额方法
    public double getBalance() {
        return balance;
    }

    // 存款方法
    public boolean deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            return true;
        }
        return false;
    }

    // 重写 withdraw 方法使它不返回值（即 void）.声明方法抛出 overdraftException 异常
    public void withdraw(double amount) throws OverdraftException {
        if (amount <= 0) {
            throw new IllegalArgumentException("Withdrawal amount must be positive.");
        }

        if (balance < amount) {
            throw new OverdraftException("Insufficient funds", amount - balance);
        }

        balance -= amount;
    }
}